<template>
  <el-dialog title="进退货" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-card class="box-card" style="">
        <div slot="header" class="clearfix" style="height: 25px">
          <span>商品信息</span>
        </div>

        <el-row>
          <el-col :span="8">商品编号:{{ dataItem.shopNumber }}</el-col>
          <el-col :span="8">商品名称:{{ dataItem.shopName }}</el-col>
          <el-col :span="8">商品库存:{{ dataItem.stockNum }}</el-col>
        </el-row>
      </el-card>

      <br><br>

      <el-form-item>
        <el-radio v-model="dataForm.type" label="1">进货</el-radio>
        <el-radio v-model="dataForm.type" label="2">退货</el-radio>
      </el-form-item>
      <el-form-item label="数量" prop="amount">
        <el-input-number v-model="dataForm.amount" controls-position="right" :min="0" :max="9999" style="width: 200px" />&nbsp;&nbsp;{{ dataItem.comName }}
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button :loading="btnLoading" type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataItem: {},
      dataForm: {
        type: '1',
        amount: 0,
        remark: ''
      },
      dataRule: {},

      btnLoading: false
    }
  },
  methods: {
    init(item) {
      this.visible = true
      
      this.dataItem = item
      this.dataForm.amount = 0
    },
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true

          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelfloorroomconsumeshop/purchase-sale`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataItem.id,
              'type': this.dataForm.type,
              'amount': this.dataForm.amount,
              'remark': this.dataForm.remark,
              'comName': this.dataItem.comName
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: this.$t('Common.OperationSuccessful'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
            
                  this.dataForm.type = '1'
                  this.dataForm.remark = ''
                  this.btnLoading = false
                }
              })
            } else {
              this.btnLoading = false
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
